package swimv2gm.client.search;

import java.io.IOException;
import java.util.*;
import swimv2gm.bl.util.*;
import javax.swing.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.UserManager;

public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		UserManager man;
		try {
			man = (UserManager)InitialContext.doLookup("UserManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		// show search keys when viewing results
		Map<String,String[]> params = request.getParameterMap();
		request.setAttribute("params",params);

		boolean exclusive = (request.getParameter("exclusive") != null) ? Boolean.parseBoolean(request
				.getParameter("exclusive")) : false;

		List<UserData> res = man.search(request.getParameter("nome"),request.getParameter("cognome"),
				request.getParameter("indirizzo"),request.getParameter("bio"),exclusive);

		if (res == null)
			res = new ArrayList<UserData>(); // TODO...

		request.setAttribute("results",res);
		request.getRequestDispatcher("search.jsp").forward(request,response);

	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
